/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.sys.dao.DepUserMapper;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.DepUser;
import com.rf.sys.model.DepUserExample;
import com.rf.sys.service.DepUserService;
import com.rf.sys.utils.PageUtils;

@Service("depUserService")
public class DepUserServiceImpl implements DepUserService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private DepUserMapper depUserMapper;//注入dao

   	/**
	 * 根据id获取数据
	 * @param depUserId
	 * @return DepUser
	 */
   	@Override
	public DepUser getDepUserByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return depUserMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据DepUser获取数据
	 * @param DepUser
	 * @return List<DepUser>
	 */
	@Override
	public List<DepUser> getDepUser(DepUser depUser) throws Exception{
		// TODO Auto-generated method stub
		DepUserExample example = new DepUserExample();
		com.rf.sys.model.DepUserExample.Criteria criteria= example.createCriteria();
		criteria.andUserIdEqualTo(depUser.getUserId());
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<DepUser> list = depUserMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<DepUser>
	 */
	@Override
	public List<DepUser> getDepUser() throws Exception{
		// TODO Auto-generated method stub
		DepUserExample example = new DepUserExample();
		com.rf.sys.model.DepUserExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return depUserMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param DepUser
	 * @return
	 */
	@Override
	public void addDepUser(DepUser depUser) throws Exception{
		// TODO Auto-generated method stub
		depUserMapper.insert(depUser);
	}

	/**
	 * 修改数据
	 * @param DepUser
	 * @param
	 */
	@Override
	public void updateDepUser(DepUser depUser) throws Exception{
		// TODO Auto-generated method stub
		depUserMapper.updateByPrimaryKeySelective(depUser);
	}
	
	/**
	 * 删除数据
	 * @param depUserId
	 * @param
	 */
	@Override
	public void deleteDepUser(String id
) throws Exception{
		// TODO Auto-generated method stub
		depUserMapper.deleteByPrimaryKey(id
);
	}

	/**
	 * 获取数据分页
	 * @param DepUser
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getDepUserPage(DepUser depUser,PageUtils pageUtils) throws Exception{
		DepUserExample example = new DepUserExample();
		com.rf.sys.model.DepUserExample.Criteria criteria= example.createCriteria();
		
		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<DepUser> list = depUserMapper.selectByExample(example);
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", list);
		return dateMap;
	}
	
	/**
	 * 根据id获取数据
	 * @param UserId
	 * @return DepUser
	 */
   	@Override
	public DepUser getDepUserByUserID(String id) throws Exception{
   		DepUserExample example = new DepUserExample();
		com.rf.sys.model.DepUserExample.Criteria criteria= example.createCriteria();
		if(id!=null && !id.equals("")){
			criteria.andUserIdEqualTo(id);
		}
		//下一条执行语句，即为要被转换的语句
		List<DepUser> list = depUserMapper.selectByExample(example);
		if(list != null && list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}
   	
   	/**
	 * 根据部门ID获取该部门下所有用户列表
	 * @author yangxiao
	 * @param depid
	 * @return List<String>
	 */
   	@Override
	public List<String> getUserListByDepID(String depid) throws Exception{
   		List<String> resultList = new ArrayList<String>();
   		DepUserExample example = new DepUserExample();
		com.rf.sys.model.DepUserExample.Criteria criteria= example.createCriteria();
		if(depid!=null && !depid.equals("")){
			criteria.andDepIdEqualTo(depid);
		}
		List<DepUser> list = depUserMapper.selectByExample(example);
		for(DepUser du:list){
			resultList.add(du.getUserId());
		}
		return resultList;
	}
}

